package com.PVZ.engine.map;

import cn.hutool.core.bean.BeanUtil;
import com.PVZ.engine.map.level.Level;
import com.PVZ.model.Entity;
import com.PVZ.model.bullet.Bullet;
import com.PVZ.model.bullet.Sun;
import com.PVZ.model.emuns.AnimeType;
import com.PVZ.model.emuns.BulletType;
import com.PVZ.model.interfaces.Gettable;
import com.PVZ.model.plant.CherryBomb;
import com.PVZ.model.plant.Plant;
import com.PVZ.model.zombie.Zombie;
import lombok.Data;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by @Author: kabuda on @Date: 2021/5/18 21:01
 * <p>
 * 单元格高度
 */
@Data
public class MapBlock {
    /**
     * 单元格宽度
     */
    public static int width = 80;
    /**
     * 单元格高度
     */
    public static int height = 100;

    private Point point;
    private int row, column;

    //单元格内是否有植物
    boolean hasPlant =false;

    List<Zombie> zombies = new ArrayList<>();

    //单元格内的子弹(豌豆之类的东西)
    List<Bullet> bullets = new ArrayList<>();
    //单元格内的植物
    List<Plant> plants = new ArrayList<>();
    List<Gettable> gettableList = new ArrayList<>();

    public Boolean hasPlant() {
        return plants.size()>0;
    }

    public MapBlock(Point point, int row, int col) {
        this.point = point;
        this.row = row;
        this.column = col;
        this.hasPlant = false;
    }

    public Boolean hasZombie(){
        return zombies.size()>0;
    }

    /**
     * 同一个单元格内是否有僵尸和子弹
     *
     * @return 是: true 否: false
     */
    public Boolean hasBothZombieAndBullet() {
        return zombies.size() > 0 && bullets.size() > 0;
    }

    public Boolean hasBothZombieAndPlant() {
        return zombies.size() > 0 && plants.size() > 0;
    }

    public void updatePlantStatus() {
        setHasPlant(this.plants.size() != 0);
    }

    public Boolean containsGettable() {

        return this.gettableList.size() != 0;
    }

    /**
     * 向单元格添加实体类对象
     *
     * @param entity 实体类
     */
    public void addEntity(Entity entity) {
        if (entity instanceof Gettable) {
            //若添加元素是可拾取类
            if (!gettableList.contains(entity)) {
                this.gettableList.add((Gettable) entity);
            }
        } else if (entity instanceof Zombie) {
            if (!zombies.contains(entity)) {
                zombies.add((Zombie) entity);
            }
        } else if (entity instanceof Plant) {
            if (!plants.contains(entity)) {
                plants.add((Plant) entity);
            }
        } else if (entity instanceof Bullet) {
            if (!bullets.contains(entity)) {
                bullets.add((Bullet) entity);
            }
        }
    }

    public void removeEntity(Entity entity) {
        if (entity instanceof Gettable) {
            //若添加元素是可拾取类
            if (!gettableList.contains(entity)) {
                this.gettableList.remove((Gettable) entity);
            }
        }
        if (entity instanceof Zombie) {
            if (!zombies.contains(entity)) {
                zombies.remove((Zombie) entity);
            }
        } else if (entity instanceof Plant) {
            if (!plants.contains(entity)) {
                plants.remove((Plant) entity);
            }
        } else if (entity instanceof Bullet) {
            if (!bullets.contains(entity)) {
                bullets.remove((Bullet) entity);
            }
        }
    }

    public void clearEntities() {
        plants.clear();
        zombies.clear();
        gettableList.clear();
        bullets.clear();
    }

//    public void getKillBoom(Zombie zombie, Level level){
//        int entityRow=zombie.getRow();
//        int entityCol=zombie.getCol();
//
//        for(int i=0;i<level.getPlantFactory().getAllPlantsList().size();i++){
//            if(level.getPlantFactory().getAllPlantsList().get(i)instanceof CherryBomb){
//                int cherryRow = level.getPlantFactory().getAllPlantsList().get(i).getRow();
//                int cherryCol = level.getPlantFactory().getAllPlantsList().get(i).getCol();
//                if(entityRow-1<=cherryRow&&cherryRow<=entityRow+1&&entityCol-1<=cherryCol&&cherryCol<=entityCol+1){
//                    zombie.health=-1;
//                }
//            }
//        }
//    }
}
